<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The BASE news class of the sub-news class.
 * MICRO, NOTE, EVENT, PHOTO.
 * 
 * These sub-news should inherit from it.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Changes $obj using reference to $cls using string name.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_util.php';
require_once INSTALLDIR . '/lib/slh_cache.php';

require_once INSTALLDIR . '/classes/Memcached_DataObject.php';

class BaseNews extends Memcached_DataObject
{
	/**
	 * Get the base News object in table 'news'.
	 */
	function getBaseNews()
	{
		return News::staticGet('id', $this->news_id);
	}
	
	/**
	 * Get the 'news'.'status'.
	 */
	function getBaseNewsStatus()
	{
		$query = 'SELECT `status` FROM `news` WHERE `id` = ' . $this->news_id;
		
		$news = new News();
		$news->query($query);
		if ($news->fetch()) {
			return $news->status;
		}
		
		return false;
	}
	
	/**
	 * Get the news type name of the news.
	 */
	function getNewsTypeName()
	{
		switch ($this->newstype) {
			case NEWS_NEWSTYPE_MICRONEWS: {
				return '微新闻';
			}
			case NEWS_NEWSTYPE_NOTENEWS: {
				return '记事新闻';
			}
			case NEWS_NEWSTYPE_EVENTNEWS: {
				return '活动';
			}
			case NEWS_NEWSTYPE_PHOTONEWS: {
				return '相册';
			}
			default: {
				return 'N/A';
			}
		}
	}
	
	/**
	 * Delete the sub news and its realted base news.
	 * Ovrriding to delete some relatives.
	 */
	function delete()
	{
		$news = $this->getBaseNews();
		$news->delete();
		
		parent::delete();
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 * 
	 * Returning the 'news_id', not the original 'id'.
	 * 
	 * FIXME: Fit to all PHP version?
	 * @param object $cls, the object(new by 'new self()') reference (self()) of the class?
	 * 		  Use 'new $cls' directly?
	 */
	static function getNewsidsByList(&$obj,
									 $offset = 0,
									 $limit = 0)
	{
		// Might throw exception.
		$subnews =&$obj;
		
		$subnews->selectAdd();
		$subnews->selectAdd('news_id');
		$subnews->whereAdd('news_id in (SELECT `id` FROM `news`)');
		$subnews->orderBy('news_id DESC');
		
		if ( !is_null($offset)) {
			$subnews->limit($offset, $limit);
		}
		
		$newsids = array();
		if ($subnews->find()) {
			while ($subnews->fetch()) {
				$newsids[] = $subnews->news_id;
			}
		}
		
		$subnews->free();
		$subnews = NULL;
		
		return $newsids;
	}
	
	/**
	 * Get the sub news objects by the given newsids.
	 * 
	 * @return ArrayWrapper, the wrappered objects.
	 * 
	 * @param object $cls, the object(new by 'new self()') reference (self()) of the class?
	 * 		  Use 'new $cls' directly?
	 */
	static function getSubnewsByNewsids($cls,
										$newsids)
	{
		$cache = Cache::instance();
		
		if ( !empty($cache)) {
			$subnews = array();
			foreach ($newsids as $newsid) {
				$x = Memcached_DataObject::staticGet($cls, 'news_id', $newsid);
				if ( !empty($x)) {
					$subnews[] = $x;
				}
			}
			
			return new ArrayWrapper($subnews);
		} else {
			// Miss the cache.
			$subnews = new $cls;
			if (empty($newsids)) {
				return $subnews;
			}
			
			$subnews->whereAdd('news_id in (' . implode(', ', $newsids) . ')');
			$subnews->find();
			
			$temp = array();
			while ($subnews->fetch()) {
				$temp[$subnews->id] = clone($subnews);
			}
			
			$wrapped = array();
			
			foreach($newsids as $newsid) {
				if (array_key_exists($newsid, $temp)) {
					$wrapped[] = $temp[$newsid];
				}
			}
			
			return new ArrayWrapper($wrapped);
		}
	}
}
?>